
package com.accenture.adf.newcodington.module18.sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * 
 * Topic: Demo serialzing and deserializing a Person object
 * 
 * Instructions:
 * Read Person data from the DAT file
 * 1. Load the Person.dat file into a File object <br/>
 * 2. Deserialize the Person.dat file to display the stored data <br/>
 * 3. Display the name, address and phone number from the Person instance restored.
 *    a. Utilize the FileInputStream and ObjectInputStream for this purpose
 * 4. Use try-catch block for IO and ClassNotFound exceptions
 *  
 */

public class DeserialFileDemo {

	public static void main(String[] args) {
		// Load the Person.dat file into a File object
		File fromFile = new File("Person.dat");
		try {
			// Deserialize the Person.dat file to display the stored data
			FileInputStream fis = new FileInputStream(fromFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Person objPerson = (Person) ois.readObject();
			System.out.println("Deserialized data: \n" + "Person Number: "
					+ objPerson.getNumber() + "\n" + "Person Name: "
					+ objPerson.getName() + "\n" + "Person Address: "
					+ objPerson.getAddress() + "\n" + "from Person.dat");
			ois.close();
		} catch (IOException e) {
			System.out.println("Deserialization failed");
			System.out.println(e);
			System.exit(1);
		} catch (ClassNotFoundException e) {
			System.out.println("Deserialization failed");
			System.out.println(e);
			System.exit(1);
		}

	}
}
